package com.itheima.health.controller.backend;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 文件上传
     */
    @PostMapping("/upload.do")
    public Result upload(MultipartFile imgFile){
        try {
            String  uploadedFlieName= imgFile.getOriginalFilename();
            String extension = uploadedFlieName.substring(uploadedFlieName.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString() + extension;
            String filePath = aliOssUtil.upload(imgFile.getBytes(), objectName);
            SetOperations setOperations = stringRedisTemplate.opsForSet();
            setOperations.add(RedisConstant.SETMEAL_PIC_RESOURCES,filePath);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
        }
        return new  Result(false,MessageConstant.PIC_UPLOAD_FAIL);
    }
    /**
     * 实现分页查询
     */
    @PostMapping("/findPage.do")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
      PageResult pageResult =  setmealService.findPage(queryPageBean);
      return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }
    /**
     * 通过id查询套餐
     */
    @GetMapping("findById.do")
    public Result findById(Integer id){
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealService.findById(id));
    }
    /**
     * 通过setMealId查询checkgroupId
     */
    @GetMapping("findCheckGroupIdsBySetmealId.do")
    public Result findCheckGroupId(Integer setmealId){
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmealService.findCheckGroupIds(setmealId));
    }
    /**
     * 新增套餐
     */
    @PostMapping("/add.do")
    public Result add(@RequestBody Setmeal setmeal,Integer checkgroupIds[]){
        setmealService.add(setmeal,checkgroupIds);
        SetOperations setOperations = stringRedisTemplate.opsForSet();
        setOperations.add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    /**
     * 修改套餐
     */
    @PostMapping("/edit.do")
    public Result update(@RequestBody Setmeal setmeal,Integer checkgroupIds[]){
        setmealService.update(setmeal,checkgroupIds);
        SetOperations setOperations = stringRedisTemplate.opsForSet();
        setOperations.add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    /**
     * 删除套餐
     */
    @GetMapping("/delete.do")
    public Result deleteById(Integer id){
        setmealService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
